package com.binguo.thirdPay.dashiPay;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.springframework.stereotype.Service;

/**
 * 
 * 		类说明：发送请求
 *
 */
@Service
public class DSSendRequest {
	
	
	 public String sendPost(String url, String param) {
			String result = "";
			OutputStream out = null;
		 	BufferedReader in = null;
			try {
				URL postUrl = new URL(url);
				HttpURLConnection connection = (HttpURLConnection)postUrl.openConnection();
				connection.setDoOutput(true);
				connection.setDoInput(true);
				connection.setRequestMethod("POST");
				connection.setRequestProperty("Content-Type", "application/json");
	            connection.setRequestProperty("accept", "application/json");
	            connection.setRequestProperty("Charset", "UTF-8");
				
				out = connection.getOutputStream();
				out.write(param.getBytes("UTF-8"));
				out.flush();


				in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
				String line;
				while ((line = in.readLine()) != null) {
					result += line;
				}
				line = null;
			} catch (MalformedURLException e) {
				System.out.println("DSSendRequest类中sendPost方法，创建URL对象异常");
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("DSSendRequest类中sendPost方法，创建URL对象连接异常");
			} finally {
				try {
					if (out != null) { out.close();}
					if (in != null) { in.close();}
				} catch (IOException ex) {
					System.out.println("DSSendRequest类中sendPost方法，关闭IO异常");
				}
			}
			return result;
		}
}